package judesart.app.client.ui.pub.home;

import judesart.app.client.controller.ArtworkController;
import judesart.app.client.controller.ArtworkControllerAsync;
import judesart.app.client.ui.pub.layout.PublicMainLayout;
import judesart.app.domain.User;
import judesart.app.shared.ArtworkShort;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.VerticalPanel;

public class HomePanel extends Composite {
	private static final int REFRESHING_INTERVAL = 5000;
	ArtworkControllerAsync artworkController = ArtworkController.Util
			.getInstance();
	private User user;
	private final Image image;
	private final VerticalPanel verticalPanel;
	private Timer timer;

	public HomePanel() {

		verticalPanel = new VerticalPanel();
		verticalPanel
				.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		verticalPanel.setSize("960px", "600px");
		verticalPanel.getElement().setId("id-jaw-HomePanel");

		HTML lblFeaturedWork = new HTML("featured work");
		lblFeaturedWork
				.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
		lblFeaturedWork.setWordWrap(false);
		lblFeaturedWork.setWidth("100%");
		lblFeaturedWork.setStyleName("jaw-home-feature");
		verticalPanel.add(lblFeaturedWork);
		verticalPanel.setCellHeight(lblFeaturedWork, "40px");
		verticalPanel.setCellWidth(lblFeaturedWork, "100%");
		verticalPanel.setCellVerticalAlignment(lblFeaturedWork,
				HasVerticalAlignment.ALIGN_MIDDLE);

		image = new Image((String) null);
		image.addStyleName("jaw-clickable");
		image.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {
				onImageClick(event);
			}
		});
		verticalPanel.add(image);
		verticalPanel.setCellHeight(image, "400px");
		verticalPanel.setCellWidth(image, "800px");
		verticalPanel.setCellHorizontalAlignment(image,
				HasHorizontalAlignment.ALIGN_CENTER);
		initWidget(verticalPanel);
	}

	private void onImageClick(ClickEvent event) {
		String artId = image.getElement().getId();
		if (artId != null && artId.trim().length() > 0) {
			History.newItem("gallery/artwork/" + artId);
		}
	}

	public void setUser(User user) {
		this.user = user;
		getRandomArtworkAsync();
		timer = new Timer() {
			@Override
			public void run() {
				getRandomArtworkAsync();
			}
		};
		timer.scheduleRepeating(REFRESHING_INTERVAL);
	}

	private void getRandomArtworkAsync() {
		artworkController.getRandomArtwork(this.user.getId(), new AsyncCallback<ArtworkShort>() {

			public void onSuccess(ArtworkShort result) {
				Log.debug("New artwork [" + result.getTitle() + "]");
				image.setUrl("/image/" + result.getImageId() + "_800x400");
				image.getElement().setId(result.getId().toString());
				PublicMainLayout.getInstance().loading(false);
			}

			public void onFailure(Throwable caught) {
				Log.error("Unable to get random artwork", caught);
			}
		});
	}

	public void stop() {
		if (timer != null) {
			timer.cancel();
		}
	}

	public void start() {
		getRandomArtworkAsync();
		if (timer != null) {
			timer.scheduleRepeating(REFRESHING_INTERVAL);
		}
	}
}
